﻿using Autofac;
using BookApi.Models;
using BookApi.Services;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

namespace BookApi
{
    class Program
    {
        static IContainer buildContainer;

        static void Main(string[] args)
        {
            var Container = new ContainerBuilder();
            Container.RegisterType<ApiService>()
                .As<IApiService>();

            buildContainer = Container.Build();

            var SearchBook = GetSearchList("诡神冢", 0, 2).Result;
            if (SearchBook?.Books?.Length > 0)
            {
                //Console.WriteLine(SearchBook.Books[0]._id);

                //var bookSource = GetSource(SearchBook.Books[0]._id).Result;
                //Console.WriteLine(bookSource.ToString());

                //string sourceId = string.Empty;
                //foreach (var item in bookSource)
                //{
                //    if (item.Name == "优质书源")
                //    {
                //        sourceId = item._id;
                //        break;
                //    }
                //}
                var bookId = SearchBook.Books[0].Id;

                var chapterList = GetChapterList(bookId).Result;
                foreach (var item in chapterList.MixToc.Chapters)
                {
                    Console.WriteLine($"{item.Title}: {item.Link}");
                }

                var chapter = GetChapter(chapterList.MixToc.Chapters[chapterList.MixToc.Chapters.Length - 1].Link).Result;
                Console.WriteLine(chapter.Content.Body);
            }

            Console.ReadKey();
        }

        // 查询书籍
        static async Task<SearchList> GetSearchList(string query, int start, int limit)
        {
            var requestUrl = $"http://api.zhuishushenqi.com/book/fuzzy-search?query={query}&start={start}&limit={limit}";

            var api = buildContainer.Resolve<IApiService>();
            var response = await api.GetAsync<SearchList>(requestUrl);
            return response;
        }

        // 得到书源
        static async Task<IList<BookSource>> GetSource(string id)
        {
            var requestUrl = $"http://api.zhuishushenqi.com/atoc?view=summary&book={id}";
            var api = buildContainer.Resolve<IApiService>();
            var response = await api.GetAsync<List<BookSource>>(requestUrl);
            return response;
        }

        // 得到章节列表
        static async Task<ChapterList> GetChapterList(string bookId)
        {
            var requestUrl = $"http://api.zhuishushenqi.com/mix-atoc/{bookId}?view=chapters";
            var api = buildContainer.Resolve<IApiService>();
            var response = await api.GetAsync<ChapterList>(requestUrl);
            return response;
        }

        // 得到小说内容
        static async Task<Chapter> GetChapter(string link)
        {
            var strUrl = WebUtility.UrlEncode(link);

            var requestUrl = $"http://chapter2.zhuishushenqi.com/chapter/{strUrl}";
            var api = buildContainer.Resolve<IApiService>();
            var response = await api.GetAsync<Chapter>(requestUrl);
            return response;
        }
    }
}
